package qiniufile

import (
	"context"
	"fmt"
	"strings"

	"mall/common/globalkey"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/api/internal/logic/media/util"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils/parse"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/pointy"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetFileListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic {
	return &GetFileListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFileListLogic) GetFileList(req *types.MediaFileListReq) (resp *types.MediaFileListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var mine *string
	if req.Ext != nil && len(strings.TrimSpace(*req.Ext)) > 0 {
		switch strings.TrimSpace(*req.Ext) {
		case "png":
			mine = pointy.GetPointer("image/png")
		case "jpg":
			mine = pointy.GetPointer("image/jpeg")
		case "gif":
			mine = pointy.GetPointer("image/gif")
		}
	}

	data, err := l.svcCtx.AttachmentRpc.BackendGetRecordList(l.ctx, &attachmentclient.RecordListReq{
		Page:     page,
		PageSize: pageSize,
		ParentId: req.ParentId,
		Diagnose: req.Diagnose,
		Key:      req.Key,
		Prefix:   req.Prefix,
		Marker:   req.Marker,
		Ids:      req.Ids,
		Mime:     mine,
		Type:     req.Type,
		OrderBy:  orderBys,
		FilterBy: filterBys,
	})
	if err != nil {
		return nil, err
	}

	resp = &types.MediaFileListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Marker: data.Marker,
		Data: types.MediaFileListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}

	for _, v := range data.List {
		var url string
		if v.Key != nil && len(*v.Key) > 0 {
			url = fmt.Sprintf("%s/%s", l.svcCtx.Config.NfsConf.StaticUrl, *v.Key)
		}
		var status uint32
		if v.DelStatus != nil && *v.DelStatus > 0 {
			status = uint32(globalkey.PostStatusTrash)
		} else if v.Status != nil && *v.Status > 0 {
			status = uint32(globalkey.PostStatusDraft)
		} else {
			status = uint32(globalkey.PostStatusPublish)
		}
		resp.Data.List = append(resp.Data.List, &types.MediaFileInfo{
			BaseUUIDInfo: types.BaseUUIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.ExpiredAt,
			},
			UserId:        v.EndUser,
			ParentId:      v.ParentId,
			Url:           &url,
			Bucket:        v.Bucket,
			FileType:      v.Type,
			MimeType:      v.MimeType,
			Status:        &status,
			Path:          v.Key,
			RestoreStatus: v.RestoreStatus,
			Size:          v.Size,
			Md5:           v.Md5,
			MetaValue:     v.MetaValue,
		})
	}

	return resp, nil
}
